package com.megvii.corelib.update.presenter;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.megvii.corelib.base.MegviiPresenter;
import com.megvii.corelib.update.UpdateManager;
import com.megvii.corelib.update.activity.UpdateActivity;
import com.megvii.corelib.update.param.UpdateActivityParam;
import com.megvii.corelib.update.service.UpdateService;
import com.megvii.corelib.util.AppUtil;

/**
 * Created by linyue on 16/3/6.
 */
public class UpdatePresenter extends MegviiPresenter<UpdateActivity>
        implements UpdateActivity.UpdateListener, UpdateManager.UpdateManagerListener
{
    private UpdateActivityParam param;

    private ServiceConnection serviceConnection;

    private UpdateManager updateManager;

    @Override
    protected void onCreated()
    {
        viewHolder.setListener(this);
        viewHolder.setDownloadProgressMax(UpdateManager.MAX_PROGRESS);
        param = viewHolder.getActivityParam();
        viewHolder.setForceUpdate(param.forceUpdate);
        viewHolder.setReturnButtonVisible(!param.forceUpdate);
        serviceConnection = new ServiceConnection()
        {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service)
            {
                UpdateService.UpdateServiceBinder binder = ((UpdateService.UpdateServiceBinder) service);
                updateManager = binder.updateManager;
                updateManager.registerListener(UpdatePresenter.this);
                viewHolder.setDownloading(updateManager.isDownloading() && !updateManager.isPaused());
                viewHolder.setDownloadProgress(updateManager.getCurrentProgress());
                binder.service.setActivityParam(param);
                updateManager.downloadAPK(param.url, param.apkFileName);
            }

            @Override
            public void onServiceDisconnected(ComponentName name)
            {
                updateManager.unregisterListener(UpdatePresenter.this);
                updateManager = null;
            }
        };
        viewHolder.bindService(new Intent(viewHolder, UpdateService.class), serviceConnection,
                Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onResume()
    {

    }

    @Override
    protected void onPause()
    {

    }

    @Override
    protected void onDestory()
    {
        super.onDestory();
        updateManager.unregisterListener(this);
        viewHolder.unbindService(serviceConnection);
    }

    @Override
    public void onClickPause()
    {
        if (!updateManager.isPaused())
        {
            viewHolder.setDownloading(false);
            updateManager.pauseDownload();
        }
        else
        {
            viewHolder.setDownloading(true);
            updateManager.resumeDownload();
        }
    }

    @Override
    public void onClickReset()
    {
        updateManager.resetDownload(param.url, param.apkFileName);
    }

    @Override
    public void onClickReturn()
    {
        viewHolder.finish();
    }

    @Override
    public void onClickRefreshNetwork()
    {

    }

    @Override
    public void onGetSpaceNeeded(String urlString, String fileName, int size)
    {

    }

    @Override
    public void onDownloadStart()
    {
        viewHolder.setDownloading(true);
    }

    @Override
    public void onDownloadProgress(int progress)
    {
        viewHolder.setDownloadProgress(progress);
    }

    @Override
    public void onDownloadCompleted(String fileName)
    {
        viewHolder.finish();
        viewHolder.setReturnButtonVisible(true);
    }

    @Override
    public void onDownloadError(String url, String fileName, int errorCode)
    {
        viewHolder.setDownloading(false);
        if (param.forceUpdate)
        {
            AppUtil.FakeExitApp(viewHolder);
        }
    }

    @Override
    public void onDownloadInterrupt()
    {
        viewHolder.setDownloading(false);
    }
}
